package com.example.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.course.bean.Comment;
import com.example.course.converter.CommentConverter;
import com.example.course.dto.CommentDTO;
import com.example.course.mapper.CommentMapper;
import com.example.course.service.CommentService;
import com.example.course.vo.CommentVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Jasmine-drs
 * @description 针对表【comment(评论表)】的数据库操作Service实现
 * @createDate 2025-04-22 15:36:40
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {

    private final CommentConverter commentConverter;

    public CommentServiceImpl(CommentConverter commentConverter) {
        this.commentConverter = commentConverter;
    }

    @Override
    public List<CommentVO> getCommentsByCourseId(Integer courseId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getCourseId, courseId);
        // 按发表时间降序排序，最新的评论在前面
        queryWrapper.orderByDesc(Comment::getCreateTime);
        List<Comment> comments = this.list(queryWrapper);
        return commentConverter.toVOList(comments);
    }

    @Override
    public CommentVO addComment(CommentDTO commentDTO) {
        Comment comment = commentConverter.toEntity(commentDTO);
        // createTime会通过@TableField(fill = FieldFill.INSERT)自动填充
        this.save(comment);
        return commentConverter.toVO(comment);
    }
}




